package net.bungaeting.client.android.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class GroupActivity extends Activity{
	
	private static final String KEY_PREF = "group";
	ListView adapter;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group);
        
        onGroup();
       }
	
	public void onGroup() {
    	final Intent groupIndent = new Intent(this, GroupManageActivity.class);
		final ArrayList<String> group = new ArrayList<String>();
		group.add("모든 연락처");
		
		Bungae_DB mHelper = new Bungae_DB(this);
		SQLiteDatabase db = mHelper.getWritableDatabase();
      
		Cursor cursor = db.rawQuery("SELECT * FROM groupInfo", null);
		startManagingCursor(cursor);
      
		SimpleCursorAdapter simpleAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, new String[] {"groupName"} ,
				new int[] { R.id.label });
		
		adapter = (ListView)findViewById(R.id.left_list);
        adapter.setAdapter(simpleAdapter);
        adapter.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        adapter.setOnItemClickListener(new ListView.OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	    		SharedPreferences pref = getSharedPreferences(KEY_PREF, MODE_PRIVATE);
	        	SharedPreferences.Editor e = pref.edit();
	        	
	        	TextView childText = (TextView) adapter.getChildAt(position).findViewById(R.id.label);
	        	
	        	e.putString("group", (String) childText.getText());
	        	e.commit();
	    		
	        	startActivity(groupIndent);
	    	}
	    });
    }
	
	public void groupManageClick(View v) {
		findViewById(R.id.check).setVisibility(View.VISIBLE);
		findViewById(R.id.complete_button).setVisibility(View.VISIBLE);
		findViewById(R.id.delete_button).setVisibility(View.VISIBLE);
		findViewById(R.id.manage_button).setVisibility(View.GONE);
	}
	
	public void groupDeleteClick(View v) {
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
    	LayoutInflater inflater = getLayoutInflater();
    	final View view = inflater.inflate(R.layout.dialog, null);
    	final Bungae_DB mHelper = new Bungae_DB(this);
    	final SQLiteDatabase db = mHelper.getWritableDatabase();
    	
    	alt_bld.setView(view);
    	
    	alt_bld.setCancelable(false).setPositiveButton("삭제", 
    			 
    		new DialogInterface.OnClickListener() { 
	    		public void onClick(DialogInterface dialog, int id) {
	    			TextView childText;
	    			for(int i=0; i<adapter.getChildCount(); i++) {
		    			if(((CheckBox)adapter.getChildAt(i).findViewById(R.id.check)).isChecked()==true) {
		    				childText = (TextView) adapter.getChildAt(i).findViewById(R.id.label);
		    				db.execSQL("DELETE FROM groupInfo WHERE groupName = '" + 
		    						childText.getText() + "';");
		    				
		    				//Toast.makeText(getApplicationContext(), childText.getText(), Toast.LENGTH_SHORT).show();
		    			}		    				
	    			}
	    			onGroup();
	    			dialog.cancel();
	    	}
	    }).setNegativeButton("아니오", 
    			 
    		new DialogInterface.OnClickListener() {
	    		public void onClick(DialogInterface dialog, int id) {
	    		dialog.cancel();
	    	}
	    }); 
    	
    	AlertDialog dialog = alt_bld.create();  
    	
    	dialog.setTitle("그룹 삭제");
    	dialog.show(); 
	}
	
	public void groupCompleteClick(View v) {
		//findViewById(R.id.check).setVisibility(View.INVISIBLE);
		findViewById(R.id.complete_button).setVisibility(View.GONE);
		findViewById(R.id.delete_button).setVisibility(View.GONE);
		findViewById(R.id.manage_button).setVisibility(View.VISIBLE);
		
		onGroup();
	}
	
	public void groupAddClick(View v) {
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
    	LayoutInflater inflater = getLayoutInflater();
    	final View view = inflater.inflate(R.layout.dialog, null);
    	final EditText editTextDialog = (EditText) view.findViewById(R.id.editTextDialog);
    	final Bungae_DB mHelper = new Bungae_DB(this);
    	final SQLiteDatabase db = mHelper.getWritableDatabase();
    	
    	alt_bld.setView(view);
    	
    	alt_bld.setCancelable(false).setPositiveButton("추가", 
    			 
    		new DialogInterface.OnClickListener() { 
	    		public void onClick(DialogInterface dialog, int id) {
	    			db.execSQL("INSERT INTO groupInfo VALUES (null, '"+ 
	    					editTextDialog.getText().toString() +"', null);");
	    			onGroup();
	    			dialog.cancel();
	    	}
	    }).setNegativeButton("아니오", 
    			 
    		new DialogInterface.OnClickListener() {
	    		public void onClick(DialogInterface dialog, int id) {
	    		dialog.cancel();
	    	}
	    }); 
    	
    	AlertDialog dialog = alt_bld.create();  
    	
    	dialog.setTitle("그룹 추가");
    	dialog.show(); 
    }
	
	public void testClick(View v) {
		Intent testIndent = new Intent(this, TestActivity.class);
		startActivity(testIndent);
	}
}
